 /*******************************************************************************
 ** Name: ihex.d
 ** Author: B. Gian James
 ** Description:                         **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License: CDDL                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the LICENSE file within             **
 ** this distribution or at $WIKI/display/ODS/License-software.              **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $INSTALL_DIR/LICENSE.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 ** Copyright (c) 2009-2013 Open Design Strategies, LLC                       **
 ** All rights reserved.                                                      **
 * ******************************************************************************/
module ihex;

import std.stdio;
import std.string;
import std.stdint;


class iHexFile
{
public:

	// parses a hex text string 'src' and decodes it into 'buffer'
	int hexStringToBin(string src, ubyte [] buffer)
	{
		ubyte [] dst = buffer;
		int ls = 0;
		uint8_t b = 0;
		foreach(char ch; src) {
			char c = ch;
			switch(c) {
				case 'a'...'f': b = (b<<4)| (c - 'a' + 0xa); break;
				case 'A'..'F': b = (b<<4)| (c - 'A' + 0xa); break;
				case '0'..'9': b = (b<<4)| (c - '0'); break;
				default:
					if (c > ' ') {
						writefln("%s: huh '%c' (%s)",__FILE__,c,src);
						return -1;
					}
					continue;
			}
			if (ls & 1)
				dst[ls] = b; b = 0;
			ls++;
		}
		return dst.length - buffer.length;
	}

	// reads iHex file 'fn', puts decoded size in 'codesz' and returns
	// a newly allocated buffer with the binary data (or null)
	ubyte[] hexFileToBin(string fn, in uint32_t codesz)
	{
		if (fn == "") return null;

		// TODO
	}

	// hex dump from ptr 'b' for 'l' bytes with string prefix 'w'
	void dump(const char * w, ubyte * b, size_t l)
	{
		uint32_t i;
		if (l < 16) {
			writeln(w,": ");
			for (i = 0; i < l; i++) writefln("%02x",b[i]);
		} else {
			writefln("%s: ",w);
			for (i = 0; i < l; i++) {
				if (!(i & 0x1f)) write("      ");
				write("%02x",b[i]);
				if ((i & 0x1f) == 0x1f) {
					writeln("  ");
				}
			}
		}
		writeln("")
	}
private:

}
